package com.test.a41;

import java.util.List;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.core.type.AnnotationMetadata;

public class A41 {
	public static void main(String[] args) {
		GenericApplicationContext context = new GenericApplicationContext();
		context.registerBean("config", Config.class); //读取自己定义的配置类 
		context.registerBean(ConfigurationClassPostProcessor.class);  //解析@Configuration注解
		context.refresh();

		for (String name : context.getBeanDefinitionNames()) {
			System.out.println(name);
		}
		
		 System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
	    System.out.println(context.getBean(Bean1.class));  //测试本项目和第三方都有相同的bean会怎样来处理
	        
	}

	@Configuration // 本项目的配置类
//	@Import({AutoConfiguration1.class,AutoConfiguration2.class})
	@Import(MyImportSelector.class)
	static class Config {
		@Bean
		public Bean1 bean1() {
			 System.out.println("11111");
			return new Bean1("本项目");
		}
	}

	/***
	 * 自定义一个
	 * @author Administrator类，需要实现DeferredImportSelector  
	 * 
	 *  ImportSelector 如果本项目和第三方都有相同的bean，默认加载第三方，然后加载本项目 ，后者会覆盖前者
	 *  DeferredImportSelector  和上边相反
	 *
	 */
	static class MyImportSelector implements  ImportSelector{
		@Override
		public String[] selectImports(AnnotationMetadata importingClassMetadata) {
          System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
           //for (String name : SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class, null)) {
        	   //读取springgboot的自动配置类 EnableAutoConfiguration
             // System.out.println(name);
          // }
           //  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); 
			//return new String[] {AutoConfiguration1.class.getName(),AutoConfiguration2.class.getName()};
            List<String> names = SpringFactoriesLoader.loadFactoryNames(MyImportSelector.class, null); 
            //这是通过读取配置文件来加载，配置文件是默认的位置    src/main/resources/META-INF/spring.factories
            return names.toArray(new String[0]); 
		}
	}

	@Configuration // 第三方的配置类
	static class AutoConfiguration1 {
		@Bean
		@ConditionalOnMissingBean //当缺失bean的时候才生效
		public Bean1 bean1() {
			return new Bean1("第三方");
		}
	}

	static class Bean1 {
		private String name;

		public Bean1() {
		}

		public Bean1(String name) {
			this.name = name;
		}

		@Override
		public String toString() {
			return "Bean1{" + "name='" + name + '\'' + '}';
		}
	}

	@Configuration // 第三方的配置类
	static class AutoConfiguration2 {
		@Bean
		public Bean2 bean2() {
			return new Bean2();
		}
	}

	static class Bean2 {

	}
}
